package com.rzl.expense.web.controller.common;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;

import com.wf.captcha.ArithmeticCaptcha;
import com.wf.captcha.base.Captcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.FastByteArrayOutputStream;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.rzl.expense.common.config.ExpenseConfig;
import com.rzl.expense.common.constant.CacheConstants;
import com.rzl.expense.common.constant.Constants;
import com.rzl.expense.common.core.domain.AjaxResult;
import com.rzl.expense.common.core.redis.RedisCache;
import com.rzl.expense.common.utils.uuid.IdUtils;
import com.rzl.expense.system.service.ISysConfigService;

/**
 * 验证码操作处理
 *
 * @author expense
 */
@RestController
public class CaptchaController
{

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private ISysConfigService configService;
    /**
     * 生成验证码
     */
    @GetMapping("/captchaImage")
    public AjaxResult getCode(HttpServletResponse response) throws IOException
    {
//        List<String> list = new ArrayList<>();
//        list.add("2023-04");
//        TwrFixedAdapter.adapterEntrance(1000001L,list);

        AjaxResult ajax = AjaxResult.success();
        boolean captchaEnabled = configService.selectCaptchaEnabled();
        ajax.put("captchaEnabled", captchaEnabled);
        if (!captchaEnabled) {
            return ajax;
        }
        new ArrayList<>();
        // 保存验证码信息
        String uuid = IdUtils.simpleUUID();
        String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + uuid;
        Captcha captcha = null;
        // 修改验证码
        String captchaType = ExpenseConfig.getCaptchaType();

        if ("math".equals(captchaType)) {
            captcha = new ArithmeticCaptcha(140,40,2);

        } else if ("char".equals(captchaType)) {
        }
        String code = captcha.text();
        redisCache.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
        // 转换流信息写出
        ajax.put("uuid", uuid);
        ajax.put("img", captcha.toBase64());
        return ajax;
    }
}
